package com.ddwl.gate.service;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.exception.ParamException;
import com.ddwl.gate.dao.IdCardVerifyRecordDao;
import com.ddwl.gate.model.IdCardVerifyRecord;
import com.ddwl.gate.util.TencentOCRUtil;
import com.ddwl.schema.bo.gate.IDOCRAuthBo;
import com.ddwl.schema.bo.gate.TencentOCRAuthBo;
import com.ddwl.schema.vo.gate.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;

/**
 * @Author wenbin
 * @Date 2020/10/14 14:23
 */
@Service
@RefreshScope
@Slf4j
public class IdCardVerifyService {

    @Resource
    private TencentOCRUtil tencentOCRUtil;
    @Resource
    private IdCardVerifyRecordDao idCardVerifyRecordDao;

    /**
     * 面部识别校验
     */
    public FaceVerifyORCResultVo faceVerify(TencentOCRAuthBo bo) throws Exception {
        ParamException.isBlank(bo.getBase64Str(),"人脸认证图片为空");
        ParamException.isBlank(bo.getName(),"姓名为空");
        ParamException.isBlank(bo.getNumber(),"身份证号为空");
        bo.setThreshold("0.3");
        bo.setLiveChk("1");
        record(bo);
        String s = tencentOCRUtil.checkIdCard(bo);
        FaceVerifyORCResultVo vo = JSON.parseObject(s, FaceVerifyORCResultVo.class);
        return vo;
    }

    /**
     * 身份证2要素校验
     */
    public VerifyORCResultVo idCardVerify(TencentOCRAuthBo bo) throws Exception {
        ParamException.isBlank(bo.getName(),"姓名为空");
        ParamException.isBlank(bo.getNumber(),"身份证号为空");
        record(bo);
        String s = tencentOCRUtil.checkIdCard(bo);
        VerifyORCResultVo vo = JSON.parseObject(s, VerifyORCResultVo.class);
        return vo;
    }


    public void record(TencentOCRAuthBo bo){
        IdCardVerifyRecord idCardVerifyRecord=new IdCardVerifyRecord();
        idCardVerifyRecord.setIsFaceVerify(bo.getIsFaceVerify());
        idCardVerifyRecord.setRequest(JSON.toJSONString(bo));
        idCardVerifyRecordDao.insert(idCardVerifyRecord);
    }

    public IdCardVo idCardOcr(IDOCRAuthBo bo) {
        ParamException.isTrue(StringUtil.isEmpty(bo.getImageBase64())&&StringUtil.isEmpty(bo.getImageUrl()),"图片不能为空");
        IdCardVo idCardVo = tencentOCRUtil.idDCardOCR(bo);
        return idCardVo;
    }

    public BizLicenseVo bizLicenseOcr(IDOCRAuthBo bo) {
        ParamException.isTrue(StringUtil.isEmpty(bo.getImageBase64())&&StringUtil.isEmpty(bo.getImageUrl()),"图片不能为空");
        BizLicenseVo bizLicenseVo = tencentOCRUtil.bizLicenseOCR(bo);
        return bizLicenseVo;
    }

    public BankCardVo bankCardOCR(IDOCRAuthBo bo) {
        ParamException.isTrue(StringUtil.isEmpty(bo.getImageBase64())&&StringUtil.isEmpty(bo.getImageUrl()),"图片不能为空");
        BankCardVo bankCardVo = tencentOCRUtil.bankCardOCR(bo);
        return bankCardVo;
    }
}
